package com.hhit.bigscreenbackmanage.controller.video;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.Response;
import com.hhit.bigscreenbackmanage.entity.video.VideoEntity;
import com.hhit.bigscreenbackmanage.service.video.VideoService;
import com.hhit.bigscreenbackmanage.vo.AdminVideoUpdateStatusVO;
import com.hhit.bigscreenbackmanage.vo.DeleteVo;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 视频监控接口
 * 2021/10/14 15:09
 * zq
 */
@RestController
@RequestMapping("/videoController")
public class VideoController {

    private static Logger log = LoggerFactory.getLogger(VideoController.class);

    @Autowired
    private VideoService videoService;

    @RequestMapping("/getVideoList")
    public Response getVideoList(@RequestBody JSONObject jsonObject) {
        try {
            return Response.createResponseBySuccess("查询成功",videoService.getVideoList(jsonObject));
        } catch (Exception e) {
            return Response.createResponseBySuccess("查询失败"+e.getMessage());
        }
    }

    @PostMapping("/saveVideo")
    public Response saveVideo(@RequestBody VideoEntity videoEntity){
        try {
            int addRes = videoService.saveVideo(videoEntity);
            if (addRes > 0){
                return Response.createResponseBySuccess("添加成功");
            }
            return Response.createResponseByError("添加失败 顺序号重复！");
        }catch (Exception e){
            log.error("VideoController.saveVideo Call Error",e);
            return Response.createResponseByError("添加异常");
        }
    }

    @PostMapping("/editVideo")
    public Response editVideo(@RequestBody VideoEntity videoEntity){
        try {
            int updateRes = videoService.editVideo(videoEntity);
            if (updateRes > 0){
                return Response.createResponseBySuccess("更新成功");
            }
//            if (updateRes == -1){
//                return Response.createResponseByError("更新失败,请求对象为空");
//            }
            return Response.createResponseByError("更新失败 顺序号重复");
        }catch (Exception e){
            log.error("VideoController.editVideo Call Error",e);
            return Response.createResponseByError("更新异常");
        }
    }

    @PostMapping("/removeVideo")
    public Response removeVideo(@RequestBody DeleteVo deleteVo){
        try {
            int deleteRes = videoService.removeVideo(deleteVo);
            if (deleteRes > 0){
                return Response.createResponseBySuccess("删除成功");
            }
            if (deleteRes == -1){
                return Response.createResponseByError("删除失败,请求对象为空");
            }
            return Response.createResponseByError("删除失败");
        }catch (Exception e){
            log.error("VideoController.removeVideo Call Error",e);
            return Response.createResponseByError("删除异常");
        }
    }

    /**
     * @Description: 加载默认天眼视频
     * @Author: qianchengcheng
     * @Date: 2021/4/19 9:02
     * @Param: []
     * @Return: Response<Map<String,Object>>
     **/
    @GetMapping("/loadDefaultVideo")
    public Response<Map<String,Object>> loadDefaultVideo(){
        try {
            Map<String,Object> result = videoService.loadDefaultVideo();
            return Response.createResponseBySuccess("查询成功",result);
        }catch (Exception e){
            log.error("loadDefaultVideo==>查询异常",e);
            return Response.createResponseByError("查询异常:"+e.getMessage());
        }
    }

    /**
     * 安全管理首次加载请求视频
     * @return
     */
    @PostMapping("/loadSafeDefaultVideo")
    public Response<Map<String,Object>> loadSafeDefaultVideo(@RequestBody JSONObject jsonObject){
        try {
            Map<String,Object> result = videoService.loadSafeDefaultVideo(jsonObject);
            return Response.createResponseBySuccess("查询成功",result);
        }catch (Exception e){
            log.error("loadSafeDefaultVideo==>查询异常",e);
            return Response.createResponseByError("查询异常:"+e.getMessage());
        }
    }

    /**
     * @Description: 加载主设备视频
     * @Author: qianchengcheng
     * @Date: 2021/4/19 9:02
     * @Param: []
     * @Return: Response<Map<String,Object>>
     **/
    @GetMapping("/loadMainEqVideo")
    public Response<Map<String,Object>> loadMainEqVideo(){
        try {
            Map<String,Object> result = videoService.loadMainEqVideo();
            return Response.createResponseBySuccess("查询成功",result);
        }catch (Exception e){
            log.error("loadDefaultVideo==>查询异常",e);
            return Response.createResponseByError("查询异常:"+e.getMessage());
        }
    }


    @PostMapping("/updateStatus")
    public Response updateStatus(@RequestBody AdminVideoUpdateStatusVO adminVideoUpdateStatus){
        if (adminVideoUpdateStatus == null || StringUtils.isEmpty(adminVideoUpdateStatus.getStatus()) || adminVideoUpdateStatus.getId() == null){
            log.info("【updateStatus】入参 {} {}", adminVideoUpdateStatus.getId(),adminVideoUpdateStatus.getStatus());
            return Response.createResponseByError("参数错误");
        }
        try {
            videoService.updateStatus(adminVideoUpdateStatus);
            return Response.createResponseBySuccess("修改成功");
        }catch (Exception e){
            log.error("VideoController.saveVideo Call Error",e);
            return Response.createResponseByError("修改失败");
        }
    }

}
